/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.crossSection.CrossSection;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    ElmCrossSectionDialog dgl = new ElmCrossSectionDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public final class ElmCrossSectionDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private FemModel fmodel;
	
	private JPanel secButPane = new JPanel();
	private String[] secLabelList;
	private JComboBox secListCombo = new JComboBox();
	private DecimalField secIxField;
	private DecimalField secIyField;
	private DecimalField secIzField;
	private DecimalField secAField;
	private DecimalField secHField;
	private DecimalField secThickField;
	public String secLabel;
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public ElmCrossSectionDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		fmodel = parent.getModel().getDiscreteModel();
		
		setUpSectionsList();
		setUpSectionsCombo();
		
		setUpFormat();
		setUpFields();
		
		//Setting subpanel cross section
		JPanel sectionPane = new JPanel();
		sectionPane.setLayout(new BoxLayout(sectionPane, BoxLayout.Y_AXIS));
		sectionPane.setMaximumSize(new Dimension(550,155));
		sectionPane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dCSDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		secListCombo.setMaximumSize(new Dimension(500,26));
		secListCombo.setPreferredSize(new Dimension(500,26));
		
		secListCombo.setMaximumRowCount(5);
		
		secIxField.setEditable(false);
		secIyField.setEditable(false);
		secIzField.setEditable(false);
		secAField.setEditable(false);
		secHField.setEditable(false);
		secThickField.setEditable(false);
		
		secListCombo.addItemListener(new SecComboListener());
		
		secButPane.add(secListCombo);
		
		JPanel secPropPane = new JPanel();
		secPropPane.setLayout(new GridLayout(2,1,0,0));
		
		JPanel secPropSPane = new JPanel();
		secPropSPane.setLayout(new GridLayout(1,3,2,2));
		secPropSPane.setPreferredSize(new Dimension(450,26));
		JPanel secPropIPane = new JPanel();
		secPropIPane.setLayout(new GridLayout(1,3,2,2));
		secPropIPane.setPreferredSize(new Dimension(450,26));
		
		JPanel secIxPane = new JPanel();
		JPanel secIyPane = new JPanel();
		JPanel secIzPane = new JPanel();
		JPanel secAPane = new JPanel();
		JPanel secHPane = new JPanel();
		JPanel secThickPane = new JPanel();
		
		secIxPane.add(new JLabel("Ix:"));
		secIxPane.add(secIxField);
		secIyPane.add(new JLabel("Iy:"));
		secIyPane.add(secIyField);
		secIzPane.add(new JLabel("Iz:"));
		secIzPane.add(secIzField);
		secAPane.add(new JLabel(parent.getResourceBundle().getString("dCSDialog02")));
		secAPane.add(secAField);
		secHPane.add(new JLabel(parent.getResourceBundle().getString("dCSDialog03")));
		secHPane.add(secHField);
		secThickPane.add(new JLabel(parent.getResourceBundle().getString("dCSDialog11")));
		secThickPane.add(secThickField);
		
		secPropSPane.add(secIxPane);
		secPropSPane.add(secIyPane);
		secPropSPane.add(secIzPane);
		secPropIPane.add(secAPane);
		secPropIPane.add(secHPane);
		secPropIPane.add(secThickPane);
		
		secPropPane.add(secPropSPane);
		secPropPane.add(secPropIPane);
		
		sectionPane.add(secButPane);
		sectionPane.add(secPropPane);
		
		setPane(sectionPane);
		
    }
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dCSDialog04"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		
		try {
			
			int secIndex = secListCombo.getSelectedIndex();
			if (secIndex>0)
				secLabel = ((CrossSection)fmodel.getSectionsList().get(secIndex-1)).getLabel();
			else secLabel = null;
			
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			Element elm = (Element)this.parent.getController().getSelection().get(0);
			
			String str;
			int num;
			
			//Setting cross section fields
			if (elm.getCrossSection() != null)
				str = elm.getCrossSection().getLabel();
			else str = "";
			num = fmodel.getNumberOfSections();
			int secIndex = 0;
			for (int i=0; i<num; i++) {
				if (str.equals(secLabelList[i+1]))
					secIndex = i+1;
			}
			secListCombo.setSelectedIndex(secIndex);
			try {
				CrossSection cs = elm.getCrossSection();
				secIxField = new DecimalField(cs.getIx(), 7, numberFormat);
				secIyField = new DecimalField(cs.getIy(), 7, numberFormat);
				secIzField = new DecimalField(cs.getIz(), 7, numberFormat);
				secAField = new DecimalField(cs.getArea(), 7, numberFormat);
				secHField = new DecimalField(cs.getHeight(), 7, numberFormat);
				secThickField = new DecimalField(cs.getThickness(), 7, numberFormat);
			} catch (Exception e) {
				secIxField = new DecimalField(0, 7, numberFormat);
				secIyField = new DecimalField(0, 7, numberFormat);
				secIzField = new DecimalField(0, 7, numberFormat);
				secAField = new DecimalField(0, 7, numberFormat);
				secHField = new DecimalField(0, 7, numberFormat);
				secThickField = new DecimalField(0, 7, numberFormat);
				secIxField.setText(" ");
				secIyField.setText(" ");
				secIzField.setText(" ");
				secAField.setText(" ");
				secHField.setText(" ");
				secThickField.setText(" ");
			}
			
		} else {
			
			//Setting cross section fields
			secIxField = new DecimalField(0, 7, numberFormat);
			secIyField = new DecimalField(0, 7, numberFormat);
			secIzField = new DecimalField(0, 7, numberFormat);
			secAField = new DecimalField(0, 7, numberFormat);
			secHField = new DecimalField(0, 7, numberFormat);
			secThickField = new DecimalField(0, 7, numberFormat);
			secListCombo.setSelectedIndex(0);
			secIxField.setText(" ");
			secIyField.setText(" ");
			secIzField.setText(" ");
			secAField.setText(" ");
			secHField.setText(" ");
			secThickField.setText(" ");
			
		}
		
	}
	
//*****************************************************************************
	
	public void setUpSectionsList() {
		
		int num = fmodel.getNumberOfSections();
		
		if (num > 0) {
			secLabelList = new String[num+1];
			secLabelList[0] = "--- " + parent.getResourceBundle().getString("dCSDialog05") + " ---";
			for (int i=0; i<num; i++)
				secLabelList[i+1] = ((CrossSection)(parent.getModel().getDiscreteModel()).getSectionsList().get(i)).getLabel();
		}else {
			secLabelList = new String[1];
			secLabelList[0] = "----- " + parent.getResourceBundle().getString("dCSDialog06") + " -----";
		}
		
	}
	
//*****************************************************************************
	
	public void setUpSectionsCombo() {
		
		secListCombo.removeAllItems();
		for (int i=0; i<secLabelList.length; i++)
			secListCombo.addItem(secLabelList[i]);
		
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class SecComboListener implements ItemListener {
		public void itemStateChanged(ItemEvent evt) {
			
			int index = secListCombo.getSelectedIndex();
			
			if (index > 0 ) {
				
				CrossSection section = (CrossSection)fmodel.getSectionsList().get(index-1);
				
				secIxField.setValue(section.getIx());
				secIyField.setValue(section.getIy());
				secIzField.setValue(section.getIz());
				secAField.setValue(section.getArea());
				secHField.setValue(section.getHeight());
				secThickField.setValue(section.getThickness());
				
			} else {
				
				secIxField.setText(" ");
				secIyField.setText(" ");
				secIzField.setText(" ");
				secAField.setText(" ");
				secHField.setText(" ");
				secThickField.setText(" ");
				
			}
			
		}
	}
	
//*****************************************************************************
}

